﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Threading.Tasks;
    using EasyWeb.Configuration;
    using EasyWeb.Data;
    using EasyWeb.Extensions;

    public static class EWCommon
    {
        public static readonly Guid CultureIdEnglish = new Guid("61d22c63-ba85-4e8d-b230-67545e6b88fe");
        public static readonly Guid CultureIdChinese = new Guid("97e46569-c16d-438c-a21b-d9c67d90d12b");
        public static readonly Guid ConfigurationIdSystemPrefix = new Guid("24664447-7293-45D5-A800-A3FE3B0ED822");
        public static readonly Guid ConfigurationIdDefaultLanguage = new Guid("EF90A8FC-44D1-4C59-B560-3F309C6F90C8");

        public static readonly Tuple<string, DbType> DatabaseUserTablePrimaryKeyField = new Tuple<string, DbType>("ew_id", DbType.Guid);
        public static readonly Tuple<string, DbType> DatabaseUserTableCreatedByField = new Tuple<string, DbType>("ew_created_by", DbType.String);
        public static readonly Tuple<string, DbType> DatabaseUserTableCreatedField = new Tuple<string, DbType>("ew_created", DbType.DateTime);
        public static readonly Tuple<string, DbType> DatabaseUserTableModifiedByField = new Tuple<string, DbType>("ew_modified_by", DbType.String);
        public static readonly Tuple<string, DbType> DatabaseUserTableModifiedField = new Tuple<string, DbType>("ew_modified", DbType.DateTime);
        // HACK: a workaround for timestamp field. using DbType.Object to represent timestamp field.
        public static readonly Tuple<string, DbType> DatabaseUserTableTimestampField = new Tuple<string, DbType>("ew_timestamp", DbType.Object);

        public static readonly IReadOnlyList<Tuple<string, DbType>> DatabaseUserTableBuiltInFields = new[]
        {
            DatabaseUserTablePrimaryKeyField,
            DatabaseUserTableCreatedByField,
            DatabaseUserTableCreatedField,
            DatabaseUserTableModifiedByField,
            DatabaseUserTableModifiedField,
            DatabaseUserTableTimestampField,
        };

        public static async Task<string> GetDatabaseUserTableNameAsync(string tableName)
        {
            EWGuard.IsNotNullOrWhiteSpace(tableName, "tableName");

            return await GetDatabaseUserObjectNameAsync(tableName);
        }

        public static async Task<string> GetDatabaseUserColumnNameAsync(string columnName)
        {
            EWGuard.IsNotNullOrWhiteSpace(columnName, "columnName");

            return await GetDatabaseUserObjectNameAsync(columnName);
        }

        private static async Task<string> GetDatabaseUserObjectNameAsync(string objectName)
        {
            var dao = EWExtensionManager.CompositionContainer.GetExportedValue<IEWConfigurationDao>();
            var config = await dao.SelectAsync<EWSystemPrefixConfiguration>();

            if (config == null || string.IsNullOrWhiteSpace(config.Value))
            {
                throw new InvalidOperationException("The system prefix is not configued.");
            }

            return string.Format("{0}{1}", config.Value, objectName);
        }
    }
}
